<%--
  Created by IntelliJ IDEA.
  User: leixiaolong
  Date: 2018/9/20
  Time: 下午5:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/include.jsp" %>

<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="/static/css/rpt-condition.css" media="all" type="text/css">

    <style>
        .layui-form-label {
            width: 160px !important;
        }
    </style>

    <style>
        /* 防止下拉框的下拉列表被隐藏---必须设置--- */
        .layui-table-cell {
            overflow: visible !important;
        }

        /* 使得下拉框与单元格刚好合适 */
        td .layui-form-select {
            /*margin-top: -10px;*/
            /*margin-left: -15px;*/
            /*margin-right: -15px;*/
            /*solid: #d2d2d2;*/
            /*border: 1px;*/
        }

        /* 设置下拉框的高度与表格单元相同 */
        .layui-input, .layui-select, .layui-textarea {
            height: 30px !important;
        }

    </style>

    <style>
        .col-select-lg {
            margin-top: 10px;
            height: 30px;
            width: 300px;
            min-width: 200px;
            max-width: 600px;
        }

        .layui-input-inline {
            min-width: 300px;
            max-width: 600px;
        }
    </style>
</head>
<body class="layui-bg-gray">
<div class="layui-row layui-col-space10" style="margin: 10px; background-color: white">
    <div class="layui-container">
        <div class="layui-row layui-form">
            <div class="layui-col-md6 ">
                <div class="layui-form-item">
                    <label class="layui-form-label ">仓库编码：</label>
                    <input type="text" id="stor_code" class="layui-input-inline dom-add-input">
                </div>
            </div>
            <div class="layui-col-md6  layui-hide">
                <div class="layui-form-item">
                    <label class="layui-form-label ">仓库信息id：</label>
                    <input type="text" id="key_id" class="layui-input-inline dom-add-input">
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">仓库名称：</label>
                    <input type="text" id="stor_name" class="layui-input-inline dom-add-input">
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">仓库简称：</label>
                    <input type="text" id="abbr_name" class="layui-input-inline dom-add-input">
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">仓库地址：</label>
                    <input type="text" id="address" class="layui-input-inline dom-add-input">
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">使用用途：</label>
                    <input type="text" id="stor_purp" class="layui-input-inline dom-add-input">
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">使用面积：</label>
                    <input type="text" id="stor_area" class="layui-input-inline dom-add-input">
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">仓库使用到期日：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="stop_date" class="layui-input-inline dom-add-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">仓库类型：</label>
                    <div class="layui-input-inline">
                        <select class="layui-form-select" id="stor_type" lay-filter="stor_type">
                            <option value="" selected>全部</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div style="margin: 10px; background-color: white;">
    <table class="layui-hide" id="tab_stor_update" lay-filter="stor-add-filter"></table>
</div>

<div class="layui-btn-container" style="float: right; margin-right: 10%">
    <button class="layui-btn layui-btn-normal layui-btn-warm" lay-submit data-type="confirm">提交</button>
    <button class="layui-btn layui-btn-normal layui-btn-warm" lay-submit data-type="cancel">取消</button>
</div>
</body>

<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-select.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/table-data.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/utils/tax-math.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/utils/base-data.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/utils/base-math.js"></script>

<script type="text/html" id="toolbarAdd">
    <div class="layui-btn-group" style="float: right; margin-right: 10px;">
        <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="add">新增</button>
    </div>
</script>

<script type="text/html" id="storInfoDel">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
    </div>
</script>

<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-select.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-date.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/utils/moment.js"></script>
<script>
    var recordNum = 0;

    layui.use(["element", "form", "layer", "table", "jquery", "laydate"], function () {
        var element = layui.element, layer = layui.layer, table = layui.table, $ = layui.jquery;
        var form = layui.form, laydate = layui.laydate;
        var nowdate = moment().format('YYYY-MM-DD');
        //初始化渲染组件
        renderDate('stop_date' , nowdate);
        var params = {
            sysCode : '29'
        }
        initCodeTableSelect("stor_type","/base/getSysCodeList",params);

        table.set({
            limit: 50
        });
        var tableIns = table.render({
            id: 'tab_stor_update',
            elem: '#tab_stor_update',
            toolbar: '#toolbarAdd',
            defaultToolbar: [],
            cellMinWidth: 90,
            data: [],
            height: 'full-350',
            cols: [[
                {field: 'id', title: '序号', align: 'center'},
                {field: 'keyId', title: '仓库联系人id', align: 'center', hide: true},
                {field: 'linkName', title: '联系人名称', align: 'center', edit:'true'},
                {field: 'linkSex', title: '联系人性别', align: 'center', edit:'true'},
                {field: 'linkPhone', title: '联系人电话', align: 'center', edit:'true'},
                {field: 'linkEmail', title: '联系人邮箱', align: 'center', edit:'true'},
                {field: 'level', title: '联系人优先级', align: 'center', edit:'true'},
                {field: 'operator', title: '操作', fixed: 'right', templet: '#storInfoDel'}]],
            done: function () {
                console.log("渲染表格 over");
                form.render();
            },
            page: false
        });

        form.render('select');

        table.on('toolbar(stor-add-filter)', function (obj) {
            switch (obj.event) {
                case 'add':
                    debugger
                    layer.msg('添加');
                    var newdata = table.cache["tab_stor_update"];
                    var numData = newdata.length;
                    var newline = {
                        id: recordNum ++
                    };

                    if(numData > 50){
                        layer.msg("仓库联系人不能超过50条！");
                        break;
                    }
                    newdata.push(newline);
                    table.reload('tab_stor_update', {
                        data: newdata
                    });

                    form.render('select');
                    break;
                case 'del':
                    layer.msg('删除');
                    break;
                case 'update':
                    layer.msg('编辑');
                    break;
            }
            ;
        });


        table.on('tool(stor-add-filter)', function (obj) {
            switch (obj.event) {
                case 'delete':
                    layer.confirm("是否删除本条记录", {icon: 3}, function (index) {
                        obj.del();
                        layer.close(index);
                    });
                    break;
            }
        });

        active = {
            confirm: function confirm() {
                var data = table.cache.tab_stor_update;

                var newdata = trimAllArray(data);

                request = {
                    "id": $('#key_id').val(),
                    "storCode": $('#stor_code').val(),
                    "storName": $('#stor_name').val(),
                    "abbrName": $('#abbr_name').val(),
                    "address": $('#address').val(),
                    "storPurp": $('#stor_purp').val(),
                    "storArea": $('#stor_area').val(),
                    "storType": $('#stor_type').find('option:selected').val(),
                    "stopDate": $('#stop_date').val(),
                    "storLinkData": newdata
                };
                var reqBody = (JSON.stringify(request));
                var isTrue = true;
                $.ajax({
                    type: "POST",
                    url: "/base/storInfoModify",
                    contentType: 'application/json',
                    dataType: 'json',//服务端返回数据类型
                    data: reqBody,
                    async: false,
                    success: function (data) {
                        if (data.rspCode == 'RSP000000') {
                            layer.msg("修改仓库信息成功,编号:" + data.data);
                        } else {
                            isTrue = false;
                            layer.msg("修改仓库信息失败:" + data.rspMsg);
                            return;
                        }
                    },
                    error: function (data) {
                        console.log("系统异常:" + data);
                        layer.msg("系统异常,请联系管理员");
                    }
                });

                if (isTrue) {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);

                    parent.layui.table.reload("tab_stor_apply", {});
                }
            },
            cancel: function cancel() {
                layer.msg("关闭弹出仓库信息录入");
                var parent_layer = parent.layer;
                var index = parent.layer.getFrameIndex(window.name); //获取当前窗体索引

                parent.layer.close(index);
                parent.layui.table.reload("tab_stor_apply", {});
            }
        };

        form.on('submit', function (data) {
            var elem = data.elem;
            active[$(this).data("type")].call(this, elem);
        });
    });

    function init(paramData) {
        var params = {id: paramData.id};
        debugger;
        layui.use(["form", "jquery","table", "laydate"], function () {
            var form = layui.form, $ = layui.jquery, table = layui.table, laydate = layui.laydate;
            $.ajax({
                url: "/base/updateStorInfoPage",
                type: 'POST',
                sync: false,
                data: params,
                success: function (data) {
                    $('#key_id').val(data.data.id);
                    $('#stor_code').val(data.data.storCode);
                    $('#stor_name').val(data.data.storName);
                    $('#abbr_name').val(data.data.abbrName);
                    $('#address').val(data.data.address);
                    $('#stor_purp').val(data.data.storPurp);
                    $('#stor_area').val(data.data.storArea);
                    $('#stor_type').val(data.data.storType);
                    renderDate('due_date',data.data.stopDate);
                    form.render('select');
                    var list = data.data.storLinkData;
                    var newdata = table.cache["tab_stor_update"];
                    $.each(list, function (i, n) {  //回显到表格
                        var newline = {
                            'id': i,
                            'keyId': list[i].id,
                            'linkName': list[i].linkName,
                            'linkSex': list[i].linkSex,
                            'linkPhone': list[i].linkPhone,
                            'linkEmail': list[i].linkEmail,
                            'level': list[i].level
                        };
                        newdata.push(newline);//右侧加入
                    });
                    table.reload("tab_stor_update", {
                        data: newdata
                    });
                    layer.msg("交易成功,页面渲染完成！");
                },
                fail: function (data) {
                    layer.msg("交易失败, message:" + data.rspMsg)
                }
            });
        });
    }
</script>
</html>
